<template>
	<view class="address-list-container">
		<!-- 地址列表 -->
		<view v-if="addresses.length > 0" class="address-list">
			<view 
				v-for="(address, index) in addresses" 
				:key="address.id" 
				class="address-item"
				@click="selectAddress(address)"
			>
				<view class="address-content">
					<view class="address-header">
						<text class="recipient-name">{{ address.recipient_name }}</text>
						<text class="phone-number">{{ address.phone_number }}</text>
						<view v-if="address.is_default" class="default-tag">默认</view>
					</view>
					<view class="address-detail">
						{{ address.province }} {{ address.city }} {{ address.district }} {{ address.detailed_address }}
					</view>
				</view>
				<view class="address-actions">
					<button class="action-btn edit-btn" @click.stop="editAddress(address)">编辑</button>
					<button class="action-btn delete-btn" @click.stop="deleteAddress(address)">删除</button>
				</view>
			</view>
		</view>

		<!-- 空状态 -->
		<view v-else class="empty-state">
			<image :src="pageImages.address_empty_image" class="empty-image"></image>
			<text class="empty-text">还没有收货地址</text>
			<text class="empty-tip">添加收货地址，享受便捷购物体验</text>
		</view>

		<!-- 添加地址按钮 -->
		<view class="add-address-btn" @click="addAddress">
			<text class="add-btn-text">+ 添加新地址</text>
		</view>
	</view>
</template>

<script>
import { get, post, showLoading, hideLoading } from '@/utils/request.js'
import { getPageImages } from '@/utils/imageConfig.js'

export default {
	data() {
		return {
			// 页面图片配置
			pageImages: {
				address_empty_image: '/static/images/empty-address.png'
			},
			addresses: [],
			selectMode: false, // 是否为选择模式
			selectedAddressId: null
		}
	},
	async onLoad(options) {
		// 检查是否为选择地址模式
		if (options.select === 'true') {
			this.selectMode = true;
			this.selectedAddressId = options.selectedId ? parseInt(options.selectedId) : null;
		}
		await this.loadPageImages();
		this.loadAddresses();
	},
	onShow() {
		// 页面显示时刷新地址列表
		this.loadAddresses();
	},
	methods: {
		// 加载页面图片配置
		async loadPageImages() {
			try {
				const images = await getPageImages('address');

				if (images.address_empty_image) {
					this.pageImages.address_empty_image = images.address_empty_image.url;
				}

				console.log('地址页面图片配置加载完成');
			} catch (error) {
				console.warn('加载页面图片配置失败，使用默认图片:', error);
			}
		},

		// 加载地址列表
		async loadAddresses() {
			try {
				showLoading('加载中...');
				const res = await get('/addresses');
				this.addresses = res.data || [];
			} catch (error) {
				console.error('加载地址列表失败:', error);
				uni.showToast({
					title: error.message || '加载失败',
					icon: 'none'
				});
			} finally {
				hideLoading();
			}
		},

		// 选择地址（选择模式下）
		selectAddress(address) {
			if (this.selectMode) {
				// 返回选中的地址
				const pages = getCurrentPages();
				const prevPage = pages[pages.length - 2];
				if (prevPage) {
					prevPage.$vm.onAddressSelected(address);
				}
				uni.navigateBack();
			}
		},

		// 添加地址
		addAddress() {
			uni.navigateTo({
				url: '/pages/address/edit'
			});
		},

		// 编辑地址
		editAddress(address) {
			uni.navigateTo({
				url: `/pages/address/edit?id=${address.id}`
			});
		},

		// 删除地址
		deleteAddress(address) {
			uni.showModal({
				title: '确认删除',
				content: `确定要删除地址"${address.recipient_name}"吗？`,
				success: async (res) => {
					if (res.confirm) {
						try {
							showLoading('删除中...');
							await post(`/addresses/${address.id}`, {}, 'DELETE');
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
							this.loadAddresses();
						} catch (error) {
							console.error('删除地址失败:', error);
							uni.showToast({
								title: error.message || '删除失败',
								icon: 'none'
							});
						} finally {
							hideLoading();
						}
					}
				}
			});
		}
	}
}
</script>

<style scoped>
.address-list-container {
	background-color: #F5F5F5;
	min-height: 100vh;
	padding-bottom: 120rpx;
}

.address-list {
	padding: 20rpx;
}

.address-item {
	background-color: #FFFFFF;
	border-radius: 16rpx;
	margin-bottom: 20rpx;
	padding: 30rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.address-content {
	flex: 1;
	margin-right: 20rpx;
}

.address-header {
	display: flex;
	align-items: center;
	margin-bottom: 16rpx;
}

.recipient-name {
	font-size: 32rpx;
	font-weight: bold;
	color: #333333;
	margin-right: 20rpx;
}

.phone-number {
	font-size: 28rpx;
	color: #666666;
	margin-right: 20rpx;
}

.default-tag {
	background-color: #D4AF37;
	color: #FFFFFF;
	font-size: 20rpx;
	padding: 4rpx 12rpx;
	border-radius: 8rpx;
}

.address-detail {
	font-size: 28rpx;
	color: #666666;
	line-height: 1.4;
}

.address-actions {
	display: flex;
	flex-direction: column;
	gap: 16rpx;
}

.action-btn {
	padding: 12rpx 24rpx;
	border-radius: 8rpx;
	font-size: 24rpx;
	border: none;
	min-width: 80rpx;
}

.edit-btn {
	background-color: #D4AF37;
	color: #FFFFFF;
}

.delete-btn {
	background-color: #FF4444;
	color: #FFFFFF;
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx 40rpx;
}

.empty-image {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 40rpx;
}

.empty-text {
	font-size: 36rpx;
	color: #333333;
	font-weight: bold;
	margin-bottom: 20rpx;
}

.empty-tip {
	font-size: 28rpx;
	color: #999999;
}

.add-address-btn {
	position: fixed;
	bottom: 40rpx;
	left: 40rpx;
	right: 40rpx;
	background-color: #D4AF37;
	border-radius: 50rpx;
	padding: 30rpx;
	text-align: center;
	box-shadow: 0 4rpx 12rpx rgba(212, 175, 55, 0.3);
}

.add-btn-text {
	color: #FFFFFF;
	font-size: 32rpx;
	font-weight: bold;
}
</style>
